package com.itheima.youdianda.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.youdianda.common.PageBean;
import com.itheima.youdianda.common.ResultData;
import com.itheima.youdianda.entity.YddUser;
import com.itheima.youdianda.service.YddUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/ydd_user")
public class YddUserController {
    @Autowired
    public YddUserService yddUserService;

    /**
     * 数据回显
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(String id){
        YddUser byId = yddUserService.getById(id);
        return ResultData.ok("",byId);
    }
    @GetMapping("/list")
        public ResultData list(String sex,String username,String mobile,Integer page,Integer limit){
        //构造分页查询器
        Page<YddUser> pegeInfo=new Page<>(page,limit);

        //创建条件构造器
        LambdaQueryWrapper<YddUser> queryWrapper=new LambdaQueryWrapper<>();
        //模糊查询
        queryWrapper.like(username!=null,YddUser::getUsername,username);
        queryWrapper.like(sex!=null,YddUser::getSex,sex);
        queryWrapper.like(mobile!=null,YddUser::getMobile,mobile);
        //添加排序条件
        queryWrapper.orderByDesc(YddUser::getId);
        Page<YddUser> page1 = yddUserService.page(pegeInfo, queryWrapper);
        PageBean<YddUser>pageBean=new PageBean<>();
        pageBean.setCount((int) page1.getTotal());
        pageBean.setCurrentPage((int)page1.getCurrent());
        pageBean.setPageSize((int)page1.getSize());
        pageBean.setTotalPages((int)page1.getPages());
        pageBean.setData(page1.getRecords());
        return ResultData.ok("",pageBean);

    }
    /**
     * 添加会员
     * @param yddUser
     * @return
     */
    @PostMapping("/add")
    public ResultData save(@RequestBody YddUser yddUser){
        yddUserService.saveMobile(yddUser);
        return ResultData.ok("",yddUser);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData delete( String id){
        boolean removeById = yddUserService.removeById(id);
        return ResultData.ok("",removeById);
    }

    /**
     * 修改
     * @param yddUser
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddUser yddUser){
        boolean updateById = yddUserService.updateById(yddUser);
        return ResultData.ok("",updateById);

    }
}
